package com.snowfield.lb47.lbconfig.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowfield.lb47.lbconfig.domain.Backserver;
import com.snowfield.lb47.lbconfig.service.IBackserverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 后端服务器Controller
 *
 * @author snowfield
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/lbconfig/backserver")
public class BackserverController extends BaseController {
    @Autowired
    private IBackserverService backserverService;

    /**
     * 查询后端服务器列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:list')")
    @GetMapping("/list")
    public TableDataInfo list(Backserver backserver) {
        startPage();
        List<Backserver> list = backserverService.selectBackserverList(backserver);
        return getDataTable(list);
    }

    /**
     * 导出后端服务器列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:export')")
    @Log(title = "后端服务器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Backserver backserver) {
        List<Backserver> list = backserverService.selectBackserverList(backserver);
        ExcelUtil<Backserver> util = new ExcelUtil<Backserver>(Backserver.class);
        util.exportExcel(response, list, "后端服务器数据");
    }

    /**
     * 获取后端服务器详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(backserverService.selectBackserverById(id));
    }

    /**
     * 新增后端服务器
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:add')")
    @Log(title = "后端服务器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Backserver backserver) {
        if (!backserverService.checkNameUnique(backserver)) {
            return AjaxResult.error("新增后端服务器 '" + backserver.getServerName() + "'失败，名称已存在");
        }
        if (!backserverService.checkIpUnique(backserver)) {
            return AjaxResult.error("新增后端服务器 '" + backserver.getServerName() + "'失败，IP地址已存在");
        }
        backserver.setCreateBy(getUsername());
        return toAjax(backserverService.insertBackserver(backserver));
    }

    /**
     * 修改后端服务器
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:edit')")
    @Log(title = "后端服务器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Backserver backserver) {
        if (!backserverService.checkNameUnique(backserver)) {
            return AjaxResult.error("修改后端服务器 '" + backserver.getServerName() + "'失败，名称已存在");
        }
        if (!backserverService.checkIpUnique(backserver)) {
            return AjaxResult.error("修改后端服务器 '" + backserver.getServerName() + "'失败，IP地址已存在");
        }
        backserver.setUpdateBy(getUsername());
        return toAjax(backserverService.updateBackserver(backserver));
    }

    /**
     * 删除后端服务器
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:backserver:remove')")
    @Log(title = "后端服务器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(backserverService.deleteBackserverByIds(ids));
    }
}
